var util = require('../../utils/util.js')
var app = getApp()
var serverUrl = 'http://skyforest.static.elab-plus.com/wepy_pro/'
Page({
    data:{
        serverUrl:serverUrl
    },
    onShareAppMessage(options) {
        return {
            title: '宁波WIII',
            imageUrl: serverUrl+"touzhibaozhang.png",
            path: '/pages/security/security?loginid='+app.globalData.single.loginid
        }
    },
    goChatList:function(e){
        var isSend = wx.getStorageSync('isSend');
        if (!isSend) {//没聊天
            wx.navigateTo({
                url: '../counselorList/counselorList'
            })
        } else {
            wx.navigateTo({
                url: '../messagesList/messagesList'
            })
        }
    },
    proto_getLoginInfo: function (options) {
        wx.request({
            url: util.url(),
            method: 'POST',
            data: util.reformParam(util.tencentSignature, { identifier: app.globalData.identifier, appId: '1400091043' }),
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (ret) {
                if (ret.data.code) {
                    console.log('获取登录信息失败，调试期间请点击右上角三个点按钮，选择打开调试');
                    wx.showToast({
                        title: ret.data.message + '[' + ret.data.code + ']',
                        icon: 'warn',
                        duration: 1500,
                    })
                    return;
                }
                console.log('获取usersig登录信息成功',app.globalData.userInfo);
                app.globalData.userSig = ret.data.single.signature;
            },
            fail: function (ret) {
                console.log('获取IM信息失败: ', ret);
                if (ret.errMsg == 'request:fail timeout') {
                    wx.showToast({
                        title: "网络请求超时，请检查网络状态",
                        icon: 'warn',
                        duration: 1500,
                    })
                }
                else{
                    wx.showToast({
                        title: ret.errMsg,
                        icon: 'warn',
                        duration: 1500,
                    })
                }
            }
        });
    },
    onShow: function(e){
        wx.setNavigationBarTitle({
            title: '投资保障'
        })
        var that = this;
        if(!app.globalData.single){
            app.login(function(obj){
                var param={
                    "nickname":app.globalData.userInfo?app.globalData.userInfo.nickName:"",
                    "mobile":"",
                    "source":"3",
                    "customerHead":app.globalData.userInfo?app.globalData.userInfo.avatarUrl:"",
                    "module":app.globalData.fromChannel||"小程序",
                    "houseid":app.globalData.houseid,
                    "appVersion":"1.0",
                    "openId":app.globalData.openid,
                    "code":obj.code||"",
                    "appId":obj.appid||"",
                    "secret":obj.secret||"",
                }
                console.log(param,'param')
                wx.request({
                    url: util.url(),
                    method: 'POST',
                    data: util.reformParam(util.mineLogin, param),
                    header: {
                        'content-type': 'application/json' // 默认值
                    },
                    success:function(res){
                        console.log("***app.login***",res);
                        if(res.data.success){
                            if(res.data.single){ // 获取信息成功
                                var data = res.data.single;
                                app.globalData.qrpictureurl = data.qrpictureurl;
                                app.globalData.tonken = data.tonken;
                                app.globalData.single = data;
                                app.globalData.loginid = data.loginid;
                                app.globalData.sessionKey=data.sessionKey;
                                app.globalData.openid = data.openId||"";
                                app.globalData.phone = wx.getStorageSync('phone');
                                app.globalData.identifier = data.id+"_"+data.loginid+"_"+data.houseid;

                                that.proto_getLoginInfo();

                                var obj={
                                    "pageid":'10941019',
                                    "keyvalue":'sfc.nb.xcx.w3.head.tzbk.tzbz.enter',
                                }
                                util.reqTrackEventObj(obj,app);
                                var obj={
                                    "pageid":'10941019',
                                    "keyvalue":'sfc.nb.xcx.w3.head.tzbk.tzbz.statetime',
                                    "usetime":"3000", // 使用时长
                                }
                                util.reqTrackEventTimeObj(obj,app);
                            }
                            else{
                                wx.showToast({
                                    title: "数据不存在,请稍后再试",
                                    icon: 'warn',
                                    duration: 1500,
                                })
                            }
                        }
                        else{
                            wx.showToast({
                                title: res.data.message,
                                icon: 'warn',
                                duration: 1500,
                            })
                        }
                    },
                    fail: function (ret) {
                        console.log('获取信息失败: ', ret);
                        if (ret.errMsg == 'request:fail timeout') {
                            wx.showToast({
                                title: "网络请求超时，请检查网络状态",
                                icon: 'warn',
                                duration: 1500,
                            })
                        }
                        else{
                            wx.showToast({
                                title: ret.errMsg,
                                icon: 'warn',
                                duration: 1500,
                            })
                        }
                    }
                });
            });
        }else{

            var obj={
                "pageid":'10941019',
                "keyvalue":'sfc.nb.xcx.w3.head.tzbk.tzbz.enter',
            }
            util.reqTrackEventObj(obj,app);
            var obj={
                "pageid":'10941019',
                "keyvalue":'sfc.nb.xcx.w3.head.tzbk.tzbz.statetime',
                "usetime":"3000", // 使用时长
            }
            util.reqTrackEventTimeObj(obj,app);
        }
    },
    onUnload(){
        util.stopTrackEventTimeObj();
    },
    onHide(){
        util.stopTrackEventTimeObj();
    },
    onLoad:function(options){
        app.globalData.fromChannel = options.loginid||'小程序';
    }
})